package www.rlicker.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.model.UserIntegral;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.service.IUserIntegralService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.StringUtil;

import com.alibaba.fastjson.JSONArray;

@Controller
@RequestMapping(value="/userintegral")
public class UserIntegralController extends BaseController{
	
	@Autowired
	private IUserIntegralService service;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	@RequestMapping("/showlist")
	public String showlist(ModelMap map, HttpServletRequest request){
		SysRole sysRole = sysRoleService.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("parent_resource_id",new Long(request.getParameter("id")));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> lists = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(lists));
		}
		map.put("id", request.getParameter("id"));
		return "userintegral/userIntegralList";
	}
	
	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request){
		 Criteria<UserIntegral> param = new Criteria<UserIntegral>();
		 if (!StringUtil.isNullOrBlank(request.getParameter("startNum"))) {
			 param.addParam("startNum", Integer.parseInt(request.getParameter("startNum")));
		 }
         if (!StringUtil.isNullOrBlank(request.getParameter("endNum"))) {
          	 param.addParam("endNum",Integer.parseInt(request.getParameter("endNum")));
         }
         if (!StringUtil.isNullOrBlank(request.getParameter("phone"))) {
        	 param.addParam("phone", request.getParameter("phone"));
         }
 	     if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
	    	param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
	     }
	     if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
	    	param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
	     }
	     List<Map<String,Object>> list = service.queryPage(param);
	     PageResult pageResult = new PageResult();
	     pageResult.setTotal(service.queryPageCount(param));
		 pageResult.setRows(list);
		 return pageResult;	
	}

	@RequestMapping("/detail")
	public String detail(String id,String resourceId,ModelMap map){
		UserIntegral integral = service.queryEntity(new Long(id));
		map.put("integral", integral);
		map.put("resourceId", resourceId);
		return "userintegral/userIntegralDetail";
	}

	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	public JsonResult update(UserIntegral integral) {
		JsonResult jr = JsonResult.newInstance();
		Criteria<UserIntegral> param = new Criteria<UserIntegral>();
		param.setRecord(integral);
		param.addParam("id", integral.getId());
		String ret = service.update(param);
		String content ="޸"+integral.getPhone()+"";
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}
}
